<template>
    <div id="all">
        <nav>
            <span class="fa fa-angle-left" @touchstart="back"></span>
            <span>编辑个人资料</span>
            <span @touchstart="save">保存</span>
        </nav>
        <div id="box">
          <div id="img-list">
            <label for="image" ref="label" id="file-label">+</label>
            <input type="file" id="image" accept="image/jpeg,image/png,image/gif" multiple @change="chooseImage($event)" ref="img"> 
          </div>
        </div>
        <div class="item">
            <span>昵称</span>
            <input type="text" v-model="name">
        </div>
        <div class="item">
            <span>性别</span>
            <input type="text" v-model="gender">
        </div>
        <div class="item">
            <span>城市</span>
            <input type="text" v-model="city">
        </div>
    </div>
</template>

<script>
export default{
    data:function(){
        return{
            name:"",
            gender:"",
            city:"",
            files:[],
            img:[]
        }
    },
    methods:{
        chooseImage:function(e){
            var fileLabel =document.getElementById("file-label");
            var imgList =document.getElementById("img-list");
            var f = e.target.files[0];
            this.files.push(f);
            var img =document.createElement("img");
            img.classList.add("images");
            img.style.width="28vw";
            img.style.height="28vw";
            img.style.marginTop="10px";
            img.style.marginRight="6px";
            var url = URL.createObjectURL(f);
            img.src = url;
            imgList.insertBefore(img,fileLabel);  
        },
        save:function(){
            if(this.name==""||this.gender==""||this.city==""){
                layer.msg("请先完善信息");
                return;
            }
            var data = new FormData();
            data.append('name',this.name);
            data.append('gender',this.gender);
            data.append('city',this.city);
            data.append('pic',this.files[0]);
            this.$store.dispatch("requestUpload",data)
            .then(res=>{
                layer.msg(res.data.msg);
                this.name = "";
                this.gender = "";
                this.city = "";
            })
        },
        back:function(){
            history.back();
        }
    }
}
</script>

<style scoped>
    #all{
        background-color: white;
    }
    nav{
        display: flex;
        justify-content: space-between;
        padding: 6px 10px;
        font-family: 黑体常规;
        border-bottom: 1px solid #ccc;
    }
    .fa-angle-left{
        font-size: 20px;
    }
    #box{
    display: flex;
    flex-wrap: wrap;
    justify-content: space-around;
    }

    #img-list input{
        display: none;
    }
    #img-list{
        margin-top: 10px;
    }
    #img-list label{
        width: 28vw;
        height: 28vw;
        border: solid 1px black;
        display: inline-block;
        font-size: 60px;
        text-align: center;
        line-height: 80px;
        vertical-align: top;
        margin-top: 10px;
        border-radius: 50%;
    }
    .item{
        
        width: 80%;
        text-align: center;
        margin: 0 auto;
        margin-top: 30px;
    }
    .item span{
        color: #888;
    }
    .item input{
        border: none;
        border-bottom: 1px solid orangered;
        margin-left: 5px;
        width: 60%;
    }
</style>